/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yan.springbootstudy;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yan.springbootstudy.service.ITestService;

@RestController
public class WelcomeController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Value("${application.message:Hello World}")
	private String message = "Hello World";
	@Resource
	ITestService testService;
//	@Resource
//    private DiscoveryClient discoveryClient;
	
	@GetMapping("/")
	public String welcome(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", this.message);
		System.out.println(message);
		return "welcome";
	}

	@RequestMapping("/foo")
	public String foo(Map<String, Object> model) {
		throw new RuntimeException("Foo");
	}
	
	@RequestMapping("/info")
	public String getServiceInfo() {
		return "a test microserver";
	}
	
	@RequestMapping("/inserttest")
	public String insertTestdb() {
		testService.insertTestData();
		return "insert success!";
	}
	
	@RequestMapping("/gettest")
	public   List<Map<String,Object>> getTestData() {
//		ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
//		logger.info("/gettest, host:" + serviceInstance.getHost() + ", service_id:" + serviceInstance.getServiceId());
		return testService.getTestData();
	}
	
//	@RequestMapping("/inserttest/{userId}")
//	public String insertTestdb(@PathVariable String userId) {
//		DataOperationContext dataOperationContext = new DataOperationContext();
//		dataOperationContext.setUserId(userId);
//		DataOperationContextHolder.setDataOperationContext(dataOperationContext);
//		testService.insertTestData();
//		return "insert success!";
//	}
//	
//	@RequestMapping("/gettest/{userId}")
//	public   List<Map<String,Object>> getTestData(@PathVariable String userId ) {
//		DataOperationContext dataOperationContext = new DataOperationContext();
//		dataOperationContext.setUserId(userId);
//		DataOperationContextHolder.setDataOperationContext(dataOperationContext);
//		return testService.getTestData();
//	}


}
